/* Copyright (c) 2009 Lemur Consulting Ltd.
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:

* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.util.*;
import java.io.*;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;

public class SearchSpeedTest {

    public static void main(String[] args) throws Exception {
        String dbname = args[0];
        int qsize = Integer.parseInt(args[1]);
        int iters = Integer.parseInt(args[2]);
        
        Vector<String> words = new Vector<String>();
        BufferedReader br = new BufferedReader(new FileReader ("usr/share/dict/british-english"));
        for(;;) {
            String line = br.readLine();
            if (line == null) break;
            if (line.indexOf('\'') == -1)
                words.add(line.toLowerCase());
        }
        System.out.println("" + words.size() + " words");
        Random rand = new Random();
        String field = "contents";

        IndexReader reader = IndexReader.open(dbname);
        Searcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer();
        QueryParser parser = new QueryParser(field, analyzer);
        int total = 0;
        
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < iters; i++) {
            String qstr = "";
            
            for (int j = 0; j < qsize; j++) {
                qstr = qstr + words.elementAt(rand.nextInt(words.size())) + " ";
            }
    
            Query query = parser.parse(qstr);

            TopDocCollector collector = new TopDocCollector(10);
            searcher.search(query, collector);
            int numTotalHits = collector.getTotalHits();
            total += numTotalHits;
            if (i % 100 == 0) {
                System.out.println("" + i + " " + query.toString(field) + " -> " + numTotalHits);
            }
        }
        long dt = System.currentTimeMillis() - t0;
        System.out.println(dt + "ms, " + ((iters * 1000) / dt) + " q/s");
        System.out.println("total matches: " + total);
    }
}